import copy from "copy-rich-text";
import { snackbar } from "./mdui";
import type { ViaWindow } from "./declarations/Via";

export const hints = {
  via: {
    name: "Via",
    html: "请粘贴到 Via 设置 > 定制 > Logo > HTML 代码 的代码框中",
    css: "请粘贴到 Via 设置 > 定制 > 高级 > 自定义 CSS 的代码框中",
  },
  yujian: {
    name: "雨见",
    html: "请粘贴到 雨见 设置 > 外观设置 > 主页设置 > Logo Html 代码 的代码框中",
    css: "请粘贴到 雨见 设置 > 外观设置 > 主页设置 > 自定义 Css 代码 的代码框中",
  },
} as const;

export const isVia = !!(globalThis as unknown as ViaWindow).via?.cmd;

export function toast(message: string, onClosed: () => void = () => void 0) {
  if (isVia) {
    (globalThis as unknown as ViaWindow).via?.toast(message);
    onClosed.call(void 0);
  } else {
    snackbar({
      autoCloseDelay: 1000,
      message,
      onClosed,
    });
  }
}

export async function copyText(
  text: string,
  success: () => void = () => {
    toast("复制成功");
  },
) {
  try {
    await copy(text, { html: true });
    success();
  } catch (error: unknown) {
    toast(`复制失败: ${String(error)}`);
  }
}

export function addTimeParameter(url: string): string {
  return url + (url.includes("?") ? "&" : "?") + String(Date.now());
}
